package demo4;



public class Tset {
    public Animal func() {

        Fish fish = new Fish("金金");
        return fish;
    }

    public void func(Animal animal) {

        System.out.println(animal.name);
    }

    public void func1(){
        //方法返回
        Animal animal = func();     //返回一个鱼对象，用animal接收，发生向上转型
        Fish fish = new Fish("哈哈");
        //方法传参
        func(fish);                //将一个子类对象作为实参传给形参animal，发生向上转型

    }
    public static void main2(String[] args) {
        //父类引用指向父类对象
        Fish fish1 = (Fish)new Animal("花花");

        //父类1引用指向其他子类对象
        Animal animal1 = new Bird("圆圆");
        if(animal1 instanceof Bird) {
            Fish fish2 =  (Fish)animal1;
        }

    }
    public static void main(String[] args) {
        Animal animal1 = new Fish("金金");
        Animal animal2= new Bird("黄黄");
        Fish fish = (Fish)animal1;
        Bird bird = (Bird)animal2;
        animal1.eat();
        animal2.eat();
    }
}
class Animal {

    String name;
    int age;

    public Animal(String name) {
        this.name = name;
    }

    public void eat() {
        System.out.println(name + "吃东西");
    }
}

class Fish extends Animal{

    public Fish(String name) {
        super(name);//调用父类的带有一个参数的构造方法，给name初始化
    }

    @Override
    public void eat() {
        System.out.println(name + "吃鱼食");
    }
}

class Bird extends Animal {

    public Bird(String name) {
        super(name);
    }
    @Override
    public void eat() {
        System.out.println(name + "吃鸟粮");
    }
}
//class Animal {
//
//    String name;
//    int age;
//
//    public Animal(String name) {
//        this.name = name;
//    }
//
//    public void eat() {
//        System.out.println(name + "吃东西");
//    }
//}
//
//class Fish extends Animal{
//
//    public Fish(String name) {
//        super(name);//调用父类的带有一个参数的构造方法，给name初始化
//    }
//
//    @Override
//    public void eat() {
//        System.out.println(name + "吃鱼食");
//    }
//}
//
//class Bird extends Animal {
//
//    public Bird(String name) {
//        super(name);
//    }
//    @Override
//    public void eat() {
//        System.out.println(name + "吃鸟粮");
//    }
//}


//class Animal {
//    String name;
//    int age;
//
//    public void eat() {
//        System.out.println(name + "吃饭");
//    }
//
//    public Animal(String name) {
//        this.name = name;
//    }
//}
//
//class Dog extends Animal {
//
//    public Dog(String name) {
//        super(name);
//    }
//}
//
//class Cat extends Animal {
//    public Cat(String name) {
//        super(name);
//    }
//}
//
//public class Tset {
//
//    public static void main(String[] args) {
//        Animal animal1 = new Dog("旺财");  //父类引用接收子类对象----->向上转型
//        Animal animal2 = new Cat("咪咪");  //父类引用接收子类对象----->向上转型
//        Dog dog = (Dog)animal1;        //子类引用接收父类引用，父类引用指向子类对象
//        Cat cat = (Cat)animal2;
//
//
//    }
//    public static void main1(String[] args) {
//        Base base = new Base();
//        Derived derived = new Derived();
//    }
//}
//
//class Base {
//
//    int a = 99;
//
//    public Base (int a) {
//        this.a = a;
//    }
//}
//
//class Derived extends Base{
//
//    int b = 88;
//
//    public Derived() {
//        super();
//    }
//}

//class Base {
//
//    int a = 99;
//
//    public void func() {
//        System.out.println("base");
//    }
//}
//
//class Derived extends Base{
//
//    int a = 88;
//
//    public void func() {
//        System.out.println("derived");
//    }
//    public void func2() {
//        System.out.println(a);
//        System.out.println(super.a);
//        func();
//        super.func();
//    }
//}
//class Base {
//
//
//    public void func(int a) {
//        System.out.println("base");
//    }
//
//}
//class Derived extends Base {
//
//
//    public void func(int a,int b) {
//        System.out.println("derived");
//    }
//
//
////    public void func(int a) {
////        System.out.println("derived");
////    }
//
//
//}


//class Animal {
//    public String name;
//    public int age;
//
//    public void eat() {
//        System.out.println("吃饭");
//    }
//}
//
//class Dog {
//    public String
//}


class  Base {
    public Base() {
        func();
    }
    public void func() {
        System.out.println("Base-->func");
    }
}

class Derived extends Base {
    private int num = 999;
    public void func() {
        System.out.println("Derived-->func");
        System.out.println(num);
    }

    public static void main(String[] args) {
        Derived derived = new Derived();
    }
}
